
class Designer < Controller
  def index e
    if !e.request.post?
      @css_files = CssFile.all.to_a.sort {|a, b| a.title <=> b.title}
    end

    if e.params['save']
      @css_files.each { |file|
        file.recieve e
      }
    end

    if e.params['add']
      file = ContentTemplate.all.select { |t| t.class_name == 'MetaCssFile'}.first.clone_thing
      file.children[0].content = e.params['new_name']
      file.save
      @css_files = CssFile.all.to_a.sort {|a, b| a.title <=> b.title}
    end

    editors = ''

    @css_files.each {|f|
      editors <<
      '<div class="foldable foldable-hide">
        <a href="#" class="foldable-title '+(f == @css_files.last ? "last" : "")+'" onclick="toggle_foldable(this.parentNode); return false;">'+f.title+'</a>' +
        '<div class="foldable-area">' + f.children[1].creator(e) + '</div>'+
      '</div>'
    }

    <<HTML
<script>
  toggle_foldable = function(foldable) {
    if (foldable.className == 'foldable foldable-hide') {
      foldable.className = 'foldable foldable-show';
    } else {
      foldable.className = 'foldable foldable-hide';
    }
    
  }
</script>
  #{Element.new(:iframe, :src => '/', :id => 'site_designer')}
<div id="css_editor">
  <form method="post">
    <input type="hidden" name="new_name" />
    <center>
      <input type="submit" name="add" value="add" onclick="this.form.elements[0].value = prompt('Jméno nového css souboru:')" />
      <input type="submit" name="save" value="save"/>
    </center>
    <br />
    #{editors}
  </form>
</div>
HTML
  end
end
